class RegReader {

    private pos: number

    private regArr: string[]


    constructor(regex: string) {
        this.regArr = regex.split('')
        this.pos = 0
    }

    public get peek(): string | null {
        if(this.pos === this.regArr.length) {
            return null
        }
        return this.regArr[this.pos]
    }

    public get next(): string | null {
        let result = this.peek
        if(this.peek){
            this.pos++
        }
        return result
    }

    public get hasNext(): boolean {
        return this.pos < this.regArr.length
    }

    public static getSubRegex(reader :RegReader): string {
        let cntParam = 1
        let result = ''
        while(reader.hasNext) {
            let str = reader.next
            if(str === '(') {
                cntParam++
            }
            else if(str === ')') {
                cntParam--
                if(!cntParam) break
            }

            result = result + str
        }

        return result
    }

    public static getParallelRegex(reader: RegReader): string {
        let result = ""
        while(reader.hasNext) {
            let ch = reader.next
            if(ch === '|' && result) {
                return result
            }
            result += ch
        }
        return result
    }

}

export default RegReader
export {RegReader}